package com.quanyan.ic.datacenter.dao.impl;

import com.quanyan.ic.datacenter.constant.Constants;
import com.quanyan.ic.datacenter.dao.AlarmsHandelReportCityDao;
import com.quanyan.ic.datacenter.http.req.ReqHandleAlarm;
import com.quanyan.ic.datacenter.http.req.ReqReport;
import com.quanyan.ic.datacenter.http.resp.RespAlarmsHandlePie;
import com.quanyan.ic.datacenter.mapper.icData.AlarmsHandelReportCityMapper;
import com.quanyan.ic.datacenter.mapper.icData.entity.AlarmsHandelReportCity;
import com.quanyan.ic.datacenter.mapper.icData.entity.AlarmsHandelReportCityExample;
import com.quanyan.ic.datacenter.model.PlaceUnitInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jian.zhou on 2017/8/23.
 */
@Repository
public class AlarmsHandelReportCityDaoImpl implements AlarmsHandelReportCityDao {

    @Autowired
    private AlarmsHandelReportCityMapper alarmsHandelReportCityMapper;



    @Override
    public  List<RespAlarmsHandlePie>  findAlarmsHandleCityPie(ReqReport reqReport){
        AlarmsHandelReportCityExample alarmsHandelReportCityExample = new AlarmsHandelReportCityExample();
        AlarmsHandelReportCityExample.Criteria criteria = alarmsHandelReportCityExample.createCriteria();
        if (reqReport.getStartDate().equals(reqReport.getEndDate()))
             criteria.andDateEqualTo(reqReport.getStartDate());
        else{
            criteria.andDateGreaterThanOrEqualTo(reqReport.getEndDate());
            criteria.andDateLessThanOrEqualTo(reqReport.getStartDate());
        }

        criteria.andTypeEqualTo(Constants.AlarmsType.alarms_type_video);
        List<AlarmsHandelReportCity> cityList =  alarmsHandelReportCityMapper.selectByExample(alarmsHandelReportCityExample);
        List<RespAlarmsHandlePie> result = new ArrayList<>();
        if ( cityList != null && cityList.size() > 0 ){
            for (AlarmsHandelReportCity db : cityList){
                RespAlarmsHandlePie respAlarmsHandlePie = new RespAlarmsHandlePie();
                respAlarmsHandlePie.setId(db.getCityId());
                respAlarmsHandlePie.setType(db.getType());
                respAlarmsHandlePie.setRemark(db.getHandleRemark());
                respAlarmsHandlePie.setCount(db.getCount());
                result.add(respAlarmsHandlePie);
            }
        }
        return result;
    }


    public void insert(PlaceUnitInfo placeUnitInfo,ReqHandleAlarm reqHandleAlarm){
        AlarmsHandelReportCityExample alarmsHandelReportCityExample = new AlarmsHandelReportCityExample();
        AlarmsHandelReportCityExample.Criteria criteria = alarmsHandelReportCityExample.createCriteria();
        criteria.andCityIdEqualTo(placeUnitInfo.getCityId());
        criteria.andHandleEqualTo(reqHandleAlarm.getType());
        criteria.andDateEqualTo(new Date());
        List<AlarmsHandelReportCity> cityList =  alarmsHandelReportCityMapper.selectByExample(alarmsHandelReportCityExample);
        if ( cityList != null && cityList.size() > 0 ){
            AlarmsHandelReportCity db = cityList.get(0);
            AlarmsHandelReportCity update = new AlarmsHandelReportCity();
            update.setId(db.getCityId());
            update.setCount(db.getCount()+1);
            alarmsHandelReportCityMapper.updateByPrimaryKeySelective(update);
        }else {
            AlarmsHandelReportCity insert = new AlarmsHandelReportCity();
            insert.setCityId(placeUnitInfo.getCityId());
            insert.setCityName(placeUnitInfo.getCityName());
            insert.setDate(new Date());
            insert.setCount(1);
            insert.setHandle(reqHandleAlarm.getType());
            insert.setHandleRemark(reqHandleAlarm.getRemark());
            alarmsHandelReportCityMapper.insertSelective(insert);
        }

    }


}
